package com.kevin.chat.controller;

import com.kevin.chat.entity.UserEntity;
import com.kevin.chat.service.UserService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    @RequestMapping("/doLogin")
    public String doLogin(HttpServletRequest request) {
        String loginName = request.getParameter("loginName");
        UserEntity user = new UserEntity();
        user.setLoginName(loginName);
        user = userService.login(user);
        if (loginName == null || loginName.equals("") || user == null) {
            return login();
        }
        request.setAttribute("user", user);
        return "main";

    }

    @RequestMapping("/")
    public String login() {
        return "login";
    }

    @RequestMapping("/register")
    public String register(HttpServletRequest request) {
        return "register";
    }

    @ResponseBody
    @RequestMapping("/doRegister")
    public String doRegister(HttpServletRequest request, HttpServletResponse response) {
        String loginName = request.getParameter("loginName");
        System.out.println("loginName==" + loginName);
        UserEntity user = new UserEntity();
        user.setLoginName(loginName);
        userService.register(user);
        return "1";
    }
}
